package manage

import (
	"github.com/gofiber/fiber/v2"
	logger "github.com/sirupsen/logrus"

	"publish-manage/internal/model"
	"publish-manage/internal/service"
	"publish-manage/pkg/constant"
	"publish-manage/pkg/domain"
	"publish-manage/pkg/server"
)

var AppPackageController = new(appPackageController)

type appPackageController struct{}

//Add POST
func (_ *appPackageController) Add(ctx *fiber.Ctx) error {
	instance := new(model.AppPackage)
	if err := ctx.BodyParser(instance); err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamParse,
			Msg:  "参数解析失败",
		})
	}
	if instance.Name == "" || instance.AppId == "" || instance.Path == "" {
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamRequired,
			Msg:  "名称/所属应用ID/部署路径必须提供",
		})
	}
	if duplicated, err := service.AppPackageService.Add(instance); err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorInternal,
			Msg:  "新增出现错误: " + err.Error(),
		})
	} else if duplicated {
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorDataExists,
			Msg:  "数据已存在",
		})
	}
	return ctx.JSON(&domain.CommonResponse{
		Data: instance,
	})
}

//Delete DELETE
func (_ *appPackageController) Delete(ctx *fiber.Ctx) error {
	id := ctx.Query("id")
	if id == "" {
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamRequired,
			Msg:  "Id必须提供",
		})
	}
	if err := service.AppPackageService.Delete(id); err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorInternal,
			Msg:  "删除出现错误: " + err.Error(),
		})
	}
	return ctx.JSON(&domain.CommonResponse{})
}

//Update PUT
func (_ *appPackageController) Update(ctx *fiber.Ctx) error {
	instance := new(model.AppPackage)
	if err := ctx.BodyParser(instance); err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamParse,
			Msg:  "参数解析失败",
		})
	}
	if instance.Id == "" {
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamRequired,
			Msg:  "Id必须提供",
		})
	}
	if err := service.AppPackageService.Update(instance); err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorInternal,
			Msg:  "更新出现错误: " + err.Error(),
		})
	}
	return ctx.JSON(&domain.CommonResponse{})
}

//List GET
func (c *appPackageController) List(ctx *fiber.Ctx) error {
	condition := new(model.AppPackage)
	if err := ctx.QueryParser(condition); err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamParse,
			Msg:  "参数解析失败!",
		})
	}
	limit, offset, orderBy, err := server.ParsePaginationInfoFromQuery(ctx)
	if err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorParamParse,
			Msg:  "参数解析失败!",
		})
	}

	pi, err := service.AppPackageService.List(condition, limit, offset, orderBy)
	if err != nil {
		logger.Error(err)
		return ctx.JSON(&domain.CommonResponse{
			Code: constant.ResponseCodeErrorInternal,
			Msg:  "服务出现异常",
		})
	}
	return ctx.JSON(&domain.CommonResponse{Data: pi})
}
